namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceDirectoryPageEdit : Page
    {
        protected TextBox DirectoryAddressTxb;
        protected TextBox DirectoryFaxTxb;
        protected TextBox DirectoryNameTxb;
        protected TextBox DirectoryPhoneTxb;
        protected DropDownList DirectoryProvinceDdl;
        protected Image iResource;
        protected Label lbDirectoryAddress;
        protected Label lbDirectoryFax;
        protected Label lbDirectoryName;
        protected Label lbDirectoryPhone;
        protected Label lbDirectoryProvince;
        protected Label lbUploadFile;
        protected LinkButton LinkButton1;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["DirectoryId"]);
                ResourceDirectory resourceDirectory = ManageResource.GetResourceDirectoryInfoViaId(id);
                this.DirectoryNameTxb.Text = resourceDirectory.DirectoryName;
                this.DirectoryAddressTxb.Text = resourceDirectory.DirectoryAddress;
                this.DirectoryPhoneTxb.Text = resourceDirectory.DirectoryPhone;
                this.DirectoryFaxTxb.Text = resourceDirectory.DirectoryFax;
                for (int i = 0; i < this.DirectoryProvinceDdl.Items.Count; i++)
                {
                    if (this.DirectoryProvinceDdl.Items[i].Value == resourceDirectory.DirectoryProvince.ToString())
                    {
                        this.DirectoryProvinceDdl.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void InitializeComponent()
        {
            this.LinkButton1.Click += new EventHandler(this.LinkButton1_Click);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.DirectoryProvinceDdl.DataSource = ManageProvince.GetProvinceNoAll();
            this.DirectoryProvinceDdl.DataTextField = "RegionName";
            this.DirectoryProvinceDdl.DataValueField = "RegionId";
            this.DirectoryProvinceDdl.DataBind();
        }

        private void LinkButton1_Click(object sender, EventArgs e)
        {
            if (base.Request.QueryString["DirectoryId"] != null)
            {
                base.Response.Redirect("~/CIS/Resource/ResourceDirectoryFileSharePage.aspx?DirectoryId=" + base.Request.QueryString["DirectoryId"].ToString(), true);
            }
            else
            {
                try
                {
                    if (this.Session["DirectoryId"] == null)
                    {
                        base.Response.Write("<script> alert('请首先保存新建信息！') </script>");
                        return;
                    }
                }
                catch (Exception)
                {
                }
                base.Response.Redirect("~/CIS/Resource/ResourceDirectoryFileSharePage.aspx?DirectoryId=" + this.Session["DirectoryId"].ToString(), true);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strDirectoryName = this.DirectoryNameTxb.Text.Trim();
            string strDirectoryAddress = this.DirectoryAddressTxb.Text.Trim();
            string strDirectoryPhone = this.DirectoryPhoneTxb.Text.Trim();
            string strDirectoryFax = this.DirectoryFaxTxb.Text.Trim();
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strDirectoryName != "")
                {
                    ManageResource.UpdateResourceDirectoryInfo(new Guid(base.Request.QueryString["DirectoryId"]), strDirectoryName, strDirectoryAddress, strDirectoryPhone, strDirectoryFax, new Guid(this.DirectoryProvinceDdl.SelectedItem.Value));
                    base.Response.Redirect("~/CIS/Resource/ResourceDirectoryPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strDirectoryName != "")
            {
                if (!ManageResource.IsExistSameResourceDirectoryInfo(strDirectoryName))
                {
                    ManageResource.InsertNewResourceDirectoryInfo(Guid.NewGuid(), strDirectoryName, strDirectoryAddress, strDirectoryPhone, strDirectoryFax, new Guid(this.DirectoryProvinceDdl.SelectedItem.Value));
                    base.Response.Redirect("~/CIS/Resource/ResourceDirectoryPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbDirectoryName.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceDirectoryPageEdit", "lbDirectoryName", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbDirectoryProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceDirectoryPageEdit", "lbDirectoryProvinceOverSea", language, path);
            }
            else
            {
                this.lbDirectoryProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceDirectoryPageEdit", "lbDirectoryProvince", language, path);
            }
            this.lbDirectoryAddress.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceDirectoryPageEdit", "lbDirectoryAddress", language, path);
            this.lbDirectoryPhone.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceDirectoryPageEdit", "lbDirectoryPhone", language, path);
            this.lbDirectoryFax.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceDirectoryPageEdit", "lbDirectoryFax", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceDirectoryPageEdit", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

